﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LCA.Community.Web.Filters;
using LCA.Community.DAL.Model;
using Microsoft.Practices.Unity;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.Web.Controllers
{
    /// <summary>
    /// Controller for managing users's greetings 
    /// </summary>
    public class GreetingsController : BaseController
    {

        /// <summary>
        /// Action for creating a new greeting message
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <param name="phrase">The phrase.</param>
        /// <returns>Partial view with the greeting phrases of the user.</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [SecurityFilter(SecurityRequisites = "Registered", SolvingActionParameter = "userID", ErrorResource = "NoAutorized")]
        [ErrorFilter(typeof(Exception), "Se produjo un error al procesar la solicitud", 1)]
        [ErrorFilter(typeof(NullReferenceException), "El usuario referenciado no existe", 2)]
        public ActionResult Create(string userID, Phrase phrase)
        {
            phrase.RowKey = Guid.NewGuid().ToString();
            phrase.Date = DateTime.Now;
            phrase.Author = UserID;
            phrase.PartitionKey = userID;
            PhrasesRepository.Add(phrase);

            var partialView = new PartialViewResult();
            partialView.ViewName = "ShowPhrases";
            partialView.ViewData["name"] = userID;
            partialView.ViewData.Model = PhrasesRepository.GetPhrasesByUser(userID);

            var group = GroupsRepository.Get(userID);
            partialView.ViewData["owner"] = group != null ? IsGroupOwner(userID) : IsOwner(userID);
            return partialView;
        }

        /// <summary>
        /// Action for removing a greeting message
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <param name="phraseID">The phrase ID.</param>
        /// <returns>Partial view with the greeting messages of the user.</returns>
        [SecurityFilter(SecurityRequisites = "Owner", SolvingActionParameter = "userID", ErrorResource = "NoAuthorized")]
        [ErrorFilter(typeof(Exception), "Se produjo un error al procesar la solicitud", 1)]
        [ErrorFilter(typeof(NullReferenceException), "El usuario o frase referenciados no existe", 2)]
        public ActionResult Remove(string userID, string phraseID)
        {
            PhrasesRepository.Delete(phraseID);

            var partialView = new PartialViewResult();
            partialView.ViewName = "ShowPhrases";
            partialView.ViewData["name"] = userID;
            partialView.ViewData.Model = PhrasesRepository.GetPhrasesByUser(userID);

            var group = GroupsRepository.Get(userID);
            partialView.ViewData["owner"] = group != null ? IsGroupOwner(userID) : IsOwner(userID);
            return partialView;
        }

        [Dependency]
        public IPhrasesRepository PhrasesRepository { get; set; }
        [Dependency]
        public IUsersRepository UsersRepository { get; set; }
    }
}
